<?php
/**
 * Created by PhpStorm.
 * User: zlkmu
 * Date: 2018/1/31
 * Time: 9:42
 */

require_once ('Player.php');
class PlayerAvatar{
    //玩家基本信息
    public $player;
    ///////////////////玩家登陆后才有的信息/////////////////////
    //玩家IP地址
    public $playerIP;
    //玩家当前房间号
    public $curRoomID = 0;
    //玩家是否在线
    public $isOnline = true;
    //玩家是否为游客
    public $isVisitor = false;

    /**
     * PlayerAvatar constructor.构造函数
     * @param $playerInfo 前台微信传过来的信息
     * @param $ip 获取的玩家IP
     * @param bool $isVisitor 是否为游客
     */
    function __construct($playerInfo, $ip,  $isVisitor = false)
    {
        $this->initPlayer($playerInfo, $ip, $isVisitor);
    }

    /**
     * 实际初始化玩家信息的函数
     * @param $playerInfo
     * @param $ip
     * @param bool $isVisitor
     */
    private function initPlayer($playerInfo, $ip, $isVisitor = false){
        $this->player = new Player($playerInfo['client_msg'], $isVisitor);
        $this->playerIP = $ip;
        $this->isOnline = true;
        $this->isVisitor = $isVisitor;
    }

    /**\
     * 获取玩家UID
     * @return int 玩家ID
     */
    function getPlayerUID(){
        return $this->player->userUid;
    }

    /**
     * 设置玩家当前房间号
     * @param $roomId 房间号
     */
    function setRoomID($roomId){
        $this->curRoomID=$roomId;
    }

    /**
     * 获取玩家当前房间号
     * @return int
     */
    function getRoomID(){
        return $this->curRoomID;
    }

    /**
     * 设置玩家房卡
     * @param $roomCard
     */
    function setRoomCard($roomCard){
        $this->playerInfo->roomCard=$roomCard;
    }

    /**
     * 获取玩家的房卡
     * @return $roomCard int房卡号
     */
    function getRoomCard(){
        return $this->playerInfo->roomCard;
    }

    /**
     * 检查玩家是否在线
     * @return bool
     */
    function isPlayerOnline(){
        return $this->isOnline;
    }

    /**
     * 玩家上线的函数
     */
    function playerOnLine(){
        $this->isOnline = true;
    }

    /**
     * 更新玩家共享次数的函数
     * @param $num
     */
    function updatePlayerShare(){
        $userModel = new User();
        //查询用户今天分享了多少次 并且间隔时间是否准确
        $r = $userModel->share($this->getPlayerUID());//返回分享结果
        $this->player->shareNum = $r[1];
        return $r[0];
    }

    /**
     * 玩家退出登录的函数
     */
    function playerLogOut(){
        $this->playerIP = "";
        $this->curRoomID = 0;
        $this->isOnline = false;
        //获取和该用户相关的连接
        $connectionID = \GatewayWorker\Lib\Gateway::getClientIdByUid($this->getPlayerUID());

        for($i = 0; $i < count($connectionID); $i++){
            \GatewayWorker\Lib\Gateway::unbindUid($connectionID[$i], $this->getPlayerUID());
            \GatewayWorker\Lib\Gateway::leaveGroup($connectionID[$i], $this->getRoomID());
        }


    }

    /**
     * 玩家实名认证的函数
     * @param $userID 用户ID
     * @param $userName 用户填写的真名
     * @param $idCard 用户填写的身份证号
     * @return bool true/false
     */
    public function realNameAuth($userID, $userName, $idCard){
        $result = $this->actualRealNameAuth($userName, $idCard);
        if($result['resp']['code'] == 0){
            $user = new User();
            $r = $user->realNameAuth($userID, $userName, $idCard);
            if($r){
                $this->player->realName = $userName;
                return true;
            }
            else{
                return false;
            }
        }
        return false;
    }

    /**
     * 实际进行实名认证过程
     * @param $userName 用户填写真名
     * @param $idCard 用户填写身份证号
     * @return mixed
     */
    private function actualRealNameAuth($userName, $idCard){
        $host = "http://idcard.market.alicloudapi.com";
        $path = "/lianzhuo/idcard";
        $method = "GET";
        $appcode = "9f58c14a94994efcb874cbd9befacd1a";//APPCode
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "cardno=$idCard&name=$userName";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $resReal=curl_exec($curl);//得到认证信息
        curl_close($curl);

        return json_decode($resReal,true);
    }

    /**
     * 玩家钻石换房卡的函数
     * @param $playerID  玩家ID
     * @param $request 客户端发来的请求
     * @return mixed array/false
     */
    function diamondToCard($request){
        $userModel = new User();
        $r = $userModel->diamondToCards($this->player->getUserID(), $request["client_msg"]);
        if($r){
            $this->player->userDiamond = $r["userDiamond"];
            $this->player->roomCard = $r["roomCard"];
            return $r;
        }else{
            return false;
        }

    }

    /**
     * 刷新用户的房卡和钻石的函数
     * @return mixed array/false
     */
    function refreshDiamondAndCard(){
        $userModel=new User();
        $r=$userModel->refreshDiamondAndCards($this->getPlayerUID());
        if($r) {
            $this->player->userDiamond = $r["userDiamond"];
            $this->player->roomCard = $r["roomCard"];
            return $r;
        }
        return false;
    }

}


?>